#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char const *argv[])
{
    // 打开管道文件
    int fd = open("myfifo", O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    char buf[1024];
    ssize_t read_num;
    // 从管道读取数据并写入标准输出
    while ((read_num = read(fd, buf, sizeof(buf))) > 0) {
        if (write(STDOUT_FILENO, buf, read_num) != read_num) {
            perror("write");
            close(fd); // 关闭文件描述符
            exit(EXIT_FAILURE);
        }
    }
    
    if (read_num == -1) {
        perror("read");
        close(fd); // 关闭文件描述符
        exit(EXIT_FAILURE);
    }

    // 关闭文件描述符
    close(fd);

    return 0;
}
